package com.tenglong.teacher.ui.common.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.tenglong.teacher.R;


/**
 * Created by Kevin on 2017/12/9.
 */

public class RoundImageView extends AppCompatImageView {


    //圆角大小，默认为10
    public int mBorderRadius = 20;
    private boolean mRotate=true;

    private Paint mPaint;
    private Paint mBoardPaint;

    // 3x3 矩阵，主要用于缩小放大
    private Matrix mMatrix;

    //渲染图像，使用图像为绘制图形着色
    private BitmapShader mBitmapShader;
    private boolean imgScale;


    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mMatrix = new Matrix();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mBoardPaint = new Paint();
        mBoardPaint.setAntiAlias(true);
        mBoardPaint.setColor(0xFF777777);
        imgScale=true;
        setScaleType(ScaleType.CENTER_INSIDE);
//        setBackgroundResource(R.drawable.bg_border1);


    }
    public void setImageScale(boolean imageScale){
        imgScale=imageScale;
        // setRotation(-90);
        // setScaleType(ScaleType.CENTER_INSIDE );

    }
    public void setImageHasRoundRect(int borderRadius){
        mBorderRadius=borderRadius;
    }
    public void setImageRotate(boolean rotate){
        mRotate=rotate;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null){
            return;
        }

        Bitmap bitmap = drawableToBitamp(getDrawable());
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale = 1.0f;
        int padding=8;
        int tpdding=padding/2;
        if(!(getScaleType()==ScaleType.CENTER_INSIDE)){
            padding=0;
            tpdding=0;
        }
        float origScale= (getWidth()-padding)*1.0f/(getHeight()-padding)*1.0f;
        float bitmapScale= bitmap.getWidth()*1.0f/bitmap.getHeight()*1.0f;
        int x;
        int y;
        if(bitmapScale-origScale>0){
            scale=(getWidth()-padding) * 1.0f / bitmap.getWidth();
            x=tpdding;
            y=(int)((getHeight()-padding) * 1.0f-bitmap.getHeight() *scale)/2+tpdding;
        }else{
            scale=(getHeight()-padding) * 1.0f / bitmap.getHeight();
            x=(int)((getWidth()-padding) * 1.0f-bitmap.getWidth() *scale)/2+tpdding;
            y=tpdding;
        }

        /*if (!(bitmap.getWidth() == getWidth() && bitmap.getHeight() == getHeight()))
        {
            // 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值；
            scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(),
                    getHeight() * 1.0f / bitmap.getHeight());
        }
        // shader的变换矩阵，我们这里主要用于放大或者缩小*/
        if(!(getScaleType()==ScaleType.CENTER_INSIDE)) {
            mMatrix.setScale(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
        }else{
            mMatrix.setScale(scale, scale);
        }

        // 设置变换矩阵
        mBitmapShader.setLocalMatrix(mMatrix);
        // 设置shader
        mPaint.setShader(mBitmapShader);
        /*canvas.drawRoundRect(new RectF(0,0,getWidth(),getHeight()), mBorderRadius, mBorderRadius,
                mBoardPaint);*/
        if(getScaleType()==ScaleType.CENTER_INSIDE) {
            canvas.translate(x,y);
            canvas.drawRect(new RectF(0,0, bitmap.getWidth()*scale , bitmap.getHeight()*scale ),
                    mPaint);
        }else{
            canvas.drawRoundRect(new RectF(0, 0, getWidth() , getHeight() ), mBorderRadius, mBorderRadius,
                    mPaint);
        }

    }


    private Bitmap drawableToBitamp(Drawable drawable)
    {
        if (drawable instanceof BitmapDrawable&&mRotate)
        {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            Matrix matrix  = new Matrix();
            matrix.setRotate(-90);
            return Bitmap.createBitmap(bd.getBitmap(),0,0,bd.getBitmap().getWidth(),bd.getBitmap().getHeight(),matrix,true);

        }
        // 当设置不为图片，为颜色时，获取的drawable宽高会有问题，所有当为颜色时候获取控件的宽高
        int w = drawable.getIntrinsicWidth() <= 0 ? getWidth() : drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight() <= 0 ? getHeight() : drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);

        return bitmap;
    }
}

